/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.tyut.hrs.comon.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.neusoft.tyut.hrs.comon.entity.Emp;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/** @author: 段静迪 */
public class JwtUtil {

    // 密钥
    private static final String SECRET = "neusoft,123";
        public static final String ROLE_KEY = "roleid";
        public static final String USER_ID_KEY = "userId";
    // 过期时间:秒
    public static final int EXPIRE = 86400;

    /**
     * 生成Token
     *
     * @param emp
     * @return String
     * @throws Exception
     */
    public static String createToken(Emp emp) throws Exception {


        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.SECOND, EXPIRE);
        Date expireDate = nowTime.getTime();

        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        String token =
                JWT.create()
                        .withHeader(map) // 头
                        .withClaim( USER_ID_KEY , emp.getEmpno())
                        .withClaim(ROLE_KEY, emp.getRoleid())
                        .withSubject("HRS系统") //
                        .withIssuedAt(new Date()) // 签名时间
                        .withExpiresAt(expireDate) // 过期时间
                        .sign(Algorithm.HMAC256(SECRET)); // 签名
        return token;
    }

    /**
     * 生成Token
     *
     * @param userId
     * @param userName
     * @return
     * @throws Exception
     */
    public static String createToken(String userId, String userName) throws Exception {
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.SECOND, EXPIRE);
        Date expireDate = nowTime.getTime();

        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        String token =
                JWT.create()
                        .withHeader(map) // 头
                        .withClaim("empno", userId)
                        .withClaim("roleid", userName)
                        .withSubject("测试") //
                        .withIssuedAt(new Date()) // 签名时间
                        .withExpiresAt(expireDate) // 过期时间
                        .sign(Algorithm.HMAC256(SECRET)); // 签名
        return token;
    }

    /**
     * 验证Token
     *
     * @param token
     * @return
     * @throws Exception
     */
    public static Map<String, Claim> verifyToken(String token) throws Exception {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        DecodedJWT jwt = null;
        try {
            jwt = verifier.verify(token);
        } catch (Exception e) {
            throw new RuntimeException("凭证已过期，请重新登录");
        }
        return jwt.getClaims();
    }

    /**
     * 解析Token
     *
     * @param token
     * @return
     */
    public static Map<String, Claim> parseToken(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaims();
    }
}
